package com.online.controller;

import com.alibaba.fastjson.JSONObject;
import com.online.entity.*;
import com.online.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/course")
public class CourseController {

    @Autowired
    TeacherService teacherService;

    @Autowired
    CourseService courseService;

    @Autowired
    CourseContactClassService courseContactClassService;

    @Autowired
    StudentClassService studentClassService;

    @Autowired
    StudentService studentService;

    /**
     * 查询当前登录老师的所有课程
     * @param teacherNumber
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCourse",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String getCourse(@RequestParam(required = false) String teacherNumber) {

        //获取当前登录对象
        Subject subject = SecurityUtils.getSubject();
        teacherNumber = (String) subject.getPrincipal();
        List<Course> courseList = new ArrayList<>();
        courseList = courseService.showCourseName(teacherNumber);

        if(teacherNumber == null) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(101,"参数为空");
            return JSONObject.toJSONString(errorJsonObject);
        }

        if(courseList.size() == 0) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(102,"暂无添加课程");
            return JSONObject.toJSONString(errorJsonObject);
        }

        if(courseList != null) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
            correctJsonObject.setData(courseList);
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(103,"查询失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 查询老师的课程列表并返回页面
     * @param teacherNumber
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/getCourseList",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String getCourseList(@RequestParam(required = false) String teacherNumber, HttpServletRequest httpServletRequest) {

        //获取当前登录对象
        Subject subject = SecurityUtils.getSubject();
        teacherNumber = (String)subject.getPrincipal();

        //查询课程列表
        List<Course> courseList = new ArrayList<>();
        courseList = courseService.showCourseName(teacherNumber);

        if(!courseList.isEmpty()) {
            httpServletRequest.setAttribute("courseList",courseList);
            return "teacher/teacher_courseList";
        }else {
            httpServletRequest.setAttribute("error","error");
            return "teacher/teacher_courseList";
        }
    }

    /**
     * 删除老师选择的课程
     * @param courseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteCourse",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String deleteARecord(@RequestParam(value = "courseId",required = false) Integer courseId) {

        Subject subject = SecurityUtils.getSubject();
        String teacherNumber = (String) subject.getPrincipal();

        //删除当前课程
        int result = courseService.deleteARecord(courseId,teacherNumber);
        result = courseContactClassService.deleteARecordByCourseId(courseId);

        if(result != 0) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("删除成功");
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(111,"删除失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 教师添加一门课程
     * @param
     * @param
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertARecord",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String insertARecord(@RequestParam(value = "courseName",required = false) String courseName,
                                @RequestParam(value = "className",required = false) String className) {

        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();

        String teacherNumber = (String)subject.getPrincipal();

//        查询此课程老师是否已添加
        Course course = courseService.selectACourse(courseName,teacherNumber);

        if(course != null) {

//           获取添加的班级id
            StudentClass studentClass = studentClassService.showAClass(teacherNumber,className);

            CourseContactClass c = courseContactClassService.selectById(course.getCourseId(),studentClass.getClassId());
            if(c != null) {
                ErrorJsonObject errorJsonObject = new ErrorJsonObject(114,"添加失败");
                return JSONObject.toJSONString(errorJsonObject);
            }

            CourseContactClass courseContactClass = new CourseContactClass();
            courseContactClass.setClassId(studentClass.getClassId());
            courseContactClass.setCourseId(course.getCourseId());
            int result = courseContactClassService.insertARecord(courseContactClass);

            if(result != 0) {
                CorrectJsonObject correctJsonObject = new CorrectJsonObject("添加成功");
                return JSONObject.toJSONString(correctJsonObject);
            }else {
                ErrorJsonObject errorJsonObject = new ErrorJsonObject(105,"添加失败");
                return JSONObject.toJSONString(errorJsonObject);
            }
        }

//        添加课程信息
        Course c = new Course();
        c.setCourseName(courseName);
        c.setTeacherNumber(teacherNumber);
        int result = courseService.inserACourse(c);

//        获取添加的课程id
        Course cou = courseService.selectACourse(courseName,teacherNumber);

//        获取添加的班级id
        StudentClass studentClass = studentClassService.showAClass(teacherNumber,className);

        CourseContactClass courseContactClass = new CourseContactClass();
        courseContactClass.setClassId(studentClass.getClassId());
        courseContactClass.setCourseId(cou.getCourseId());
        result = courseContactClassService.insertARecord(courseContactClass);

        if(result != 0) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("添加成功");
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(103,"添加失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 展现学生的课程列表
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/getStudentCourseList",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String getStudentCourse(HttpServletRequest httpServletRequest) {

        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();

        //得到登录学生的studentNumber
        String studentNumber = (String) subject.getPrincipal();

        //查询学生的信息
        Student student = studentService.showByStudentNumber(studentNumber);

        //得到学生的班级信息
        String studentClass = student.getStudentClass();

        //根据班级的名字查询所对应的班级id
        List<Integer> classIdList = studentClassService.showIdByClassName(studentClass);

        //根据班级id查询班级对应的所有课程id
        List<Integer> courseIdList = new ArrayList<>();
        for(Integer classId : classIdList) {
            List<Integer> list= courseContactClassService.selectCourseIdByClassId(classId);

            for(Integer l : list) {
                courseIdList.add(l);
            }
        }


        if(courseIdList == null) {
            httpServletRequest.setAttribute("result","暂未添加任何课程");
            return "student/student_course";
        }

        //存储课程信息
        List<Course> courseList = new ArrayList<>();

        for(Integer list:courseIdList) {
            Course course = courseService.selectACourseByCourseId(list);
            courseList.add(course);
        }

        //查询teacher的详细情况
        List<Teacher> teacherList = new ArrayList<>();
        for(Course list : courseList) {
            String teacherNumber = list.getTeacherNumber();
            Teacher teacher = teacherService.showATeacher(teacherNumber);
            teacherList.add(teacher);
        }

        httpServletRequest.setAttribute("courseList",courseList);
        httpServletRequest.setAttribute("teacherList",teacherList);
        return "student/student_course";
    }

    /**
     * 查询教师所授课程的班级
     * @param courseName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findClass",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String findClass(@RequestParam(value = "courseName",required = false) String courseName) {

        Subject subject = SecurityUtils.getSubject();
        String teacherNumber = (String) subject.getPrincipal();

        Course course = courseService.selectACourse(courseName,teacherNumber);

        course.getCourseId();

        List<CourseContactClass> list = courseContactClassService.selectClassId(course.getCourseId());
        List<StudentClass> studentClassList = new ArrayList<>();

        for(CourseContactClass l : list) {
            studentClassList.add(studentClassService.showClassDetailById(l.getClassId()));
        }

        if(!studentClassList.isEmpty()) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
            correctJsonObject.setData(studentClassList);
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(113,"查询失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }
}
